package game;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Enemy extends Sprite{
	public final static long serialVersionUID = 1L;
	protected Rectangle bounds;
	protected boolean movingRight;

	public Enemy() {}
	
	public Enemy(Level g, int x, int y, String type, int speed, float scale){
		try{
		level = g;
		this.x = x;
		this.y = y;
		frames = new ImageIcon[6];
		frames[0] = new ImageIcon(ImageIO.read(new File("images/monster.png")));
		frames[1] = new ImageIcon(ImageIO.read(new File("images/monster_1.png")));
		frames[2] = new ImageIcon(ImageIO.read(new File("images/monster_2.png")));
		frames[3] = new ImageIcon(ImageIO.read(new File("images/monster_4.png")));
		frames[4] = new ImageIcon(ImageIO.read(new File("images/monster_5.png")));
		frames[5] = new ImageIcon(ImageIO.read(new File("images/monster_6.png")));
		animations = new int[4][];
		animations[0] = new int[1];
		animations[1] = new int[1];
		animations[2] = new int[4];
		animations[3] = new int[4];
		animations[0][0] = 0;
		animations[1][0] = 5;
		animations[2][0] = 0;
		animations[2][1] = 1;
		animations[2][2] = 0;
		animations[2][3] = 2;
		animations[3][0] = 5;
		animations[3][1] = 3;
		animations[3][2] = 5;
		animations[3][3] = 4;
		currentAnim = 0;
		currentFrame = 0;
		slowDown = 6;
		x = 50;
		y = 100;
		prevX = x;
		prevY = y;
		downMotion = 0;
		moveBounds = new Rectangle();
		this.scale = scale;
		this.speed = speed;
		xMotion = 0;
		fxSpeed = speed;
		curWidth = (int)(width*scale);
		curHeight = (int)(height*scale);
		movingRight = true;
		setAnim(3);
		}
		catch (IOException e) {}

}
	@Override 
	public void update(){
		prevX = x;
		prevY = y;
		nextFrame();
		move();
		calculateXPosition();
		calculateYPosition();
		createMovebounds();
	}
	
	public void move(){
		if(movingRight){
			xMotion = speed;
		}else{
			xMotion = -speed;
		}
	}
	
	//calculate the new yPosition
	protected void calculateYPosition(){
		downMotion += fxGravity;
		y += downMotion;
		//so that mario doesn't go offscreen
		if(y < -level.canvas.maxHeight+level.canvas.height){
			y = -level.canvas.maxHeight+level.canvas.height;
			downMotion = 0;
		}else if(y > level.canvas.height-curHeight){
			y = level.canvas.height-curHeight;
			downMotion = 0;
		}
	}
	
	protected void calculateXPosition(){
		x += xMotion;
		if(x + curWidth > level.canvas.maxWidth){
			x = level.canvas.maxWidth-curWidth;
			movingRight = false;
			setAnim(2);
		}else if(x < 0){
			x = 0;
			movingRight = true;
			setAnim(3);
		}
	}
	
	public void resetEffects(){
		fxSpeed = speed;
		fxGravity = level.canvas.gravity;
		fxFriction = level.canvas.friction;
	}
	
	//creates movebounds for the sprite
	protected void createMovebounds(){
		if(prevX < x){//moved right
			if(prevY < y){ // if moved down
				moveBounds.setBounds(prevX, prevY, x-prevX+curWidth, y-prevY+curHeight+1);
			}else{
				moveBounds.setBounds(prevX, y, x-prevX+curWidth, prevY-y+curHeight+1);
			}
		}else{
			if(prevY < y){
				moveBounds.setBounds(x, prevY, prevX-x+curWidth, y-prevY+curHeight+1);
			}else{
				moveBounds.setBounds(x, y, prevX-x+curWidth, prevY-y+curHeight+1);
			}
		}
	}
	
	protected void switchDirection(){
		if(movingRight == true){
			movingRight = false;
			setAnim(2);
		}else{
			movingRight = true;
			setAnim(3);
		}
	}
	public void calculateIntersectEffects(GameRectangle rect){}
	
	public void leftHit(GameRectangle g){
		super.leftHit(g);
		if(g instanceof Mario){
			level.add(new DeathAnimation(level, (int)x+(width/2), (int)y+(height/2)));
			level.mario.setBounds(0, 0, 0, 0);
		}else{
		movingRight = true;
		setAnim(3);
		}
	}
	
	public void rightHit(GameRectangle g){
		super.rightHit(g);
		if(g instanceof Mario){
			level.add(new DeathAnimation(level, (int)x+(width/2), (int)y+(height/2)));
			level.mario.setBounds(0, 0, 0, 0);
		}else{
		movingRight = false;
		setAnim(2);
		}
	}
	
	public void removeSelfFromGame(){
		level.enemies.remove(this);
	}
	
	public void topHit(GameRectangle g){
		if(g instanceof Mario){
			removeSelfFromGame();
		}
	}
}
